<template>
    <div class="bigbox">
        <Loading :show="show"></Loading>
        <div class="detail">
            <div class="border" v-show="allDataInfo.length > 0">
            </div>
            <div class="item everyItem" v-for="(item, i) in allDataInfo" @click="viewDetail(item, i)"
                :class="i == indexOf ? 'active' : ''">
                <h2>{{ item.name }}</h2>
                <h3>{{ item.subtitle }}</h3>
                <a-tag :color="i == indexOf ? 'blue' : 'green'">{{ item.update_time }}</a-tag>
            </div>
        </div>
        <ul class="info">
            <li v-for="(item, i) in detailInfo" v-slide-in="{ distance: 30, time: 500 }">
                <h1>
                    <a :href="item.url" target="_blank">
                        <span class="serialNumber" :style="{ 'opacity': computerBackColor(i) }">{{ i + 1
                            }}</span>
                        {{ item.title }}
                    </a>
                    <span v-show="item.hot" class="hot">实时：{{ item.hot }}</span>
                </h1>
            </li>
        </ul>
    </div>
</template>

<script setup lang="ts">
import { onMounted, ref, nextTick } from 'vue'
import { allNews } from '@/api/index'
const show = ref(false)
const allDataInfo: any = ref([])
const detailInfo: any = ref()
const indexOf = ref(0)
const getData = async () => {
    try {
        const result = await allNews()
        allDataInfo.value = result.data
        detailInfo.value = result.data[0].data
        show.value = true
        nextTick(() => {
            computerDistanceStyle()
        })
    } catch (error) {
        show.value = true
    }
}
const viewDetail = (el: any, index: number) => {
    computerDistanceStyle(index)
    detailInfo.value = []
    setTimeout(() => {
        detailInfo.value = el.data
    }, 200);
    indexOf.value = index
}
const computerBackColor = (i: number): string => {
    let transparent: any = (30 - i * 5) / 30
    if (transparent < 0.3) transparent = 0.3
    return transparent
}
const computerDistanceStyle = (index: number = 0) => {
    const border: any = document.querySelector('.border')
    const items: any = document.querySelectorAll('.everyItem')
    const distance = Math.abs(border.offsetTop - items[index].offsetTop)
    border.style.setProperty('--s', distance / 1000 + 's');
    border.style.setProperty('--t', items[index].offsetTop + 'px');
    border.style.setProperty('--w', items[index].offsetWidth + 'px');
    border.style.setProperty('--h', items[index].offsetHeight + 'px');
}
onMounted(async () => {
    getData()
})
</script>

<style lang="scss" scoped>
.bigbox {
    height: 100%;
    width: 100%;
    position: relative;
    display: grid;
    grid-template-columns: 200px 1fr;

    .detail {
        position: relative;
        display: grid;
        grid-template-columns: 1fr;
        gap: 10px;
        padding: 10px;
        background-color: var(--backColor);
        height: 100%;
        overflow-y: auto;
        @include scroll-bar(white, rgba(135, 207, 235, 0.529));

        .item {
            border-radius: 10px;
            text-align: center;
            cursor: pointer;
            padding: 10px;
            box-shadow:
                2px 3px 10px rgba(128, 128, 128, 0.174),
                -1px 2px 5px rgb(226, 216, 216);

            &:hover {
                color: #1177ff;
                box-shadow:
                    2px 3px 10px rgba(48, 123, 244, 0.3),
                    -1px 2px 5px rgb(142, 160, 198);
            }
        }

        .active {
            color: $primary;
            box-shadow:
                2px 3px 10px rgba(48, 123, 244, 0.3),
                -1px 2px 5px rgb(142, 160, 198);
        }

        .border {
            --h: 10px;
            transition: all var(--s);
            padding: 10px;
            border-radius: 10px;
            width: var(--w);
            height: var(--h);
            border: 3px solid var(--themeColor);
            position: absolute;
            top: var(--t);
            left: 50%;
            transform: translateX(-50%);
        }
    }

    .info {
        height: 100%;
        overflow-y: auto;
        @include scroll-bar();
        display: grid;
        grid-template-columns: 1fr 1fr;
        width: 100%;
        list-style-type: none;
        gap: 10px;
        padding: 10px;

        li {
            padding: 10px;
            box-shadow:
                2px 3px 5px rgba(128, 128, 128, 0.1),
                -1px 2px 5px rgb(226, 216, 216),
                -1px -2px 5px rgba(128, 128, 128, 0.1);

            h1 {
                display: grid;
                grid-template-columns: 1fr 150px;

                a {
                    color: var(--backColorText);

                    .serialNumber {
                        width: 20px;
                        background-color: var(--themeColor);
                        height: 20px;
                        line-height: 20px;
                        border-radius: 50%;
                        text-align: center;
                        font-size: 15px;
                        color: white;
                        display: inline-block;
                    }

                    &:hover {
                        color: $primary;
                    }
                }

                .hot {
                    padding-left: 20px;
                }
            }
        }
    }
}
</style>